﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;

namespace Actuals.Data
{
    public class DalSprint : Dal<ISprint, Sprint>, IDalSprint
    {
        private readonly IDalManager _manager;
        private static readonly IDalSprint _instance = new DalSprint();
        private const string SP_GET_SPRINT = "usp_GetSprint";
        private const string SP_GET_SPRINTS = "usp_GetSprints";
        private const string SP_GET_SPRINTWITHRELEASE = "usp_GetSprintWithRelease";
        private const string SP_ADD_AND_UPDATE_SPRINT = "usp_AddAndUpdateSprint";
        private const string SP_DELETE_SPRINT = "usp_DeleteSprint";
         private const string SP_GETSPRINTS_WITH_SPRINTID = "usp_GetSprints_With_SprintId";

        //private const string SP_GET_CURRENT_SPRINTS = "usp_GetSprints";

          public static IDalSprint Instance
        {
            get { return _instance; }
        }

        public DalSprint()
        {
            _manager = new DalManager();
        }

        public DalSprint(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ISprint obj, DataRow row)
        {
            obj.ProjectId = _manager.GetValue<int>(row, "ProjectId");
            obj.ReleaseId = _manager.GetValue<int>(row, "ReleaseId");
            obj.SprintId = _manager.GetValue<int>(row, "SprintId");
            obj.SprintDescription = _manager.GetValue<string>(row, "SprintDescription");
            obj.SprintStartDate = _manager.GetValue<DateTime>(row, "SprintStartDate");
            obj.SprintEndDate = _manager.GetValue<DateTime>(row, "SprintEndDate");
            obj.Status = _manager.GetValue<int>(row, "Status");
        }

        public ISprint GetSprint(int sprintId)
        {
            _manager.StoredProcedureName = SP_GET_SPRINT;
            _manager.AddInParameter("@SprintId", DbType.Int32, sprintId);
          

            ISprint  sprint = new Sprint();
            Convert(sprint, _manager.ExecuteDataSet());
            return sprint;
        }

        public ISprint GetSprintWithRelease(int sprintId, int releaseId)
        {
            _manager.StoredProcedureName = SP_GET_SPRINTWITHRELEASE;
            _manager.AddInParameter("@SprintId", DbType.Int32, sprintId);
            _manager.AddInParameter("@ReleaseId",DbType.Int32, releaseId);
            
            ISprint sprint = new Sprint();
            Convert(sprint, _manager.ExecuteDataSet());
            return sprint;
        }



        public IList<ISprint> GetSprints(int projectId, int releaseid)
        {
            _manager.StoredProcedureName = SP_GET_SPRINTS;
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            _manager.AddInParameter("@Releaseid", DbType.Int32, releaseid);


            List<ISprint> sprints = new List<ISprint>();
            foreach (ISprint  sprint in ConvertAll(_manager.ExecuteDataSet()))
                sprints.Add(sprint);

            return sprints;
        }


        public int DeleteSprint(int sprintId)
        {

            _manager.StoredProcedureName = SP_DELETE_SPRINT;
            _manager.AddInParameter("@SprintId", DbType.Int32, sprintId);
            _manager.ExecuteNonQuery();
            return 1;
        }

        public int AddSprint(ISprint sprint)
        {
            _manager.StoredProcedureName = SP_ADD_AND_UPDATE_SPRINT;
            _manager.AddInParameter("@ProjectId", DbType.Int32, sprint.ProjectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, sprint.ReleaseId);
            _manager.AddInParameter("@SprintId", DbType.Int32, sprint.SprintId);            
            _manager.AddInParameter("@SprintDescription", DbType.String, sprint.SprintDescription);
            _manager.AddInParameter("@SprintStartDate", DbType.DateTime, sprint.SprintStartDate);
            _manager.AddInParameter("@SprintEndDate", DbType.DateTime, sprint.SprintEndDate);
            _manager.AddInParameter("@Status", DbType.Int32, sprint.Status);



            return _manager.ExecuteNonQuery();


        }

      
    

      
    }
}
